<?php
declare (strict_types = 1);
namespace app\course\model\offline\admin;

use app\course\model\base\admin\Course as BaseCourse;
use think\model\concern\SoftDelete;

/**
 * 点播课程--后台管理
 */
class Offline extends BaseCourse
{
    use SoftDelete;
    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        'mhm_id'           => 'integer',
        'teacher_id'       => 'integer',
        'update_time'      => 'timestamp',
        'listing_time'     => 'timestamp',
        'fullcategorypath' => 'json',
    ];
    /**
     * 全局查询范围
     * @var [type]
     */
    protected $globalScope = ['courseType', 'mhm'];

    /**
     * 定义查询设备类型范围条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeCourseType($query)
    {
        $query->partition('p3');
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 如果不是平台时, 加入机构条件限制
        if (!request()->is_platform) {
            $query->where('mhm_id', request()->mhm_id);
        }
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @return   string
     */
    public static function onBeforeInsert($course)
    {
        // 点播课程类型
        $course->course_type = 3;
        $course->mhm_id      = $course->getAttr('mhm_id') ?: request()->mhm_id;
        $course->admin_user_id = request()->admin_user_id;
    }

    /**
     * 创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @param    [type]                         $course [description]
     * @return   [type]                               [description]
     */
    public static function onAfterInsert($course)
    {

    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @param    [type]                         $course [description]
     * @return   [type]                               [description]
     */
    public static function onAfterDelete($course)
    {

    }
}
